// src/store/modules/loading.store.ts
import type { Canceler } from 'axios'

export default {
    state: () => ({
        loadingCount: 0,
        controllers: new Map<string, Canceler>()
    }),

    getters: {
        isLoading: (state) => state.loadingCount > 0
    },

    actions: {
        addLoading() {
            this.loadingCount++
        },

        subLoading() {
            if (this.loadingCount > 0) this.loadingCount--
        },

        addController(controller: Canceler) {
            const id = Date.now().toString()
            this.controllers.set(id, controller)
            return id
        },

        removeController(id: string) {
            this.controllers.delete(id)
        },

        clearAllControllers() {
            this.controllers.forEach(controller => controller())
            this.controllers.clear()
        }
    }
}